package com.shixianjun.sort;

import com.shixianjun.选择排序;

import java.util.Arrays;
import java.util.Comparator;

/*排序验证器*/
public class SortVerifier {
    public static void main(String[] args) {

        SortAnalysisReport report = verifierSortResults(100, new 选择排序(), 10000, 20);
        System.out.print(report.resultPass ? "本次测试 排序验证通过\n": "本次测试 排序验证未通过\n");
        System.out.print(report.isStable ? "本次测试 结果稳定\n": "本次测试 结果不稳定\n");
        report.print(false,true, false);
    }

    public static SortAnalysisReport verifierSortResults(int times, Sortable sorter, int caseLength, int maxValue) {

        SortAnalysisReport report = new SortAnalysisReport();
        report.cases = new SortCase[times];

        report.startTime = System.currentTimeMillis();

        for (int i = 0; i < times; i++) {
            SortCase aCase = new SortCase();
            aCase.startTime = System.currentTimeMillis();
            aCase.original = SortableElement.randomSortableElementArray(caseLength, maxValue, true);
            aCase.customResult = Arrays.copyOf(aCase.original, aCase.original.length);
            aCase.systemResult = Arrays.copyOf(aCase.original, aCase.original.length);
            sorter.sort(aCase.customResult);
            Arrays.sort(aCase.systemResult, new Comparator<SortableElement>() {
                @Override
                public int compare(SortableElement o1, SortableElement o2) {
                    return o1.value - o2.value;
                }
            });

            aCase.endTime = System.currentTimeMillis();
            aCase.duration = aCase.endTime - aCase.startTime;

            report.addCase(aCase, i);
        }
        report.endTime = System.currentTimeMillis();
        report.duration = report.endTime - report.startTime;
        return report;
    }


}
